<template>
  <div class="container">
    <div class="cont-bg">
      <img src="@/assets/bg.png" alt class="bg">
    </div>
    <div class="cont">
      <inform />
      <div class="sign">恭喜您答对全部题目！获得一次抽奖机会！</div>
      <div class="middle">
        <img src="@/assets/bottom.png" alt class="bot-img">
        <img src="@/assets/top1.png" alt class="top-img">
        <div class="start" @click="submit()" />
        <div class="prise">
          <div>
            <transition name="run">
              <div v-if="isShow" style="height:100px;padding-top: 25px;">
                <div class="leve">{{ jiangpin.prize_level }} 等奖</div>
                <div class="name">{{ jiangpin.prize }}</div>
              </div>
            </transition>
            <transition name="run">
              <div v-if="!isShow" style="height:100px;padding-top: 25px;">
                <div class="leve">{{ jiangpin.prize_level }} 等奖</div>
                <div class="name">{{ jiangpin.prize }}</div>
              </div>
            </transition>
          </div>
        </div>
        <!-- <div class="residue">剩余次数: {{ numRes }}</div> -->
      </div>
      <div class="rule">
        <div class="title">
          <div class="border" />
          <div class="wz">规则说明</div>
          <div class="border" />
        </div>
        <div class="list">·答题全部正确才可抽奖, 当日抽奖次数次日清零；</div>
        <div class="list">·每日更新奖品赞助商，中奖率为100%</div>
        <div class="list">·获奖后可在首页-我的奖品中查看兑奖说明</div>
        <div class="list">·请注意奖品的使用有效期，避免过期</div>
      </div>
      <div v-show="show" class="fix" @click="routerLink">
        <div class="cont-tk">
          <div class="title">
            恭喜中奖!
            <br>请在首页-我的奖品中查看详情
          </div>
          <img src="@/assets/close.png" alt class="close">
        </div>
      </div>
    </div>
  </div>
</template>
<script>
import { userLotteryNum, userDraw, prizeToday } from '@/api/utils'
import inform from '@/components/inform'
import { Toast } from 'vant'
export default {
    components: {
        inform: inform
    },
    data() {
        return {
            off: true,
            show: false,
            numRes: 3,
            isShow: false,
            timer: null,
            timer_2: null,
            jiangpin: {
                id: 2,
                prize_level: '*******', // 奖品等级
                prize: '**************' // 奖品名称
            }
        }
    },
    async created() {
        // const numRes = await userLotteryNum()
        // this.numRes = numRes.data
        // console.log(process.env.NODE_ENV)
        // if (process.env.NODE_ENV === 'development') {
        //     this.numRes = 3
        // }
        // console.log(numRes)
    },
    methods: {
        async submit() {
            if (this.off === false) return
            this.off = false
            clearInterval(this.timer)
            clearInterval(this.timer_2)
            if (this.numRes <= 0) {
                Toast({ type: 'danger', message: '剩余抽奖次数不足' })
                return
            }
            const res = await prizeToday()
            const prizeList = res.data || []
            if (prizeList.length === 0) {
                Toast({ type: 'danger', message: '今日奖品未添加' })
                return
            }
            this.numRes--
            this.timer = setInterval(() => {
                this.isShow = !this.isShow
            }, 500)
            this.jiangpin = {
                id: 0,
                prize_level: '*******', // 奖品等级
                prize: '**************' // 奖品名称
            }
            // const drawRes = await userDraw()

            userDraw().then(drawRes => {
                console.log(drawRes)
                this.timer_2 = setTimeout(() => {
                    clearInterval(this.timer)
                    this.jiangpin = drawRes.data
                    if (drawRes.code === 0) {
                        this.jiangpin = {
                            id: 0,
                            prize_level: '5', // 奖品等级
                            prize: '谢谢参与' // 奖品名称
                        }
                    }
                    this.show = true
                }, 3000)
            }).catch(drawErr => {
                this.jiangpin = {
                    id: 0,
                    prize_level: '5', // 奖品等级
                    prize: '谢谢参与' // 奖品名称
                }
            })
        },
        closeMask() {

        },
        routerLink() {
            this.$router.replace({
                name: 'home'
            })
        }
    }
}
</script>
<style>
.run-enter-active,
.run-leave-active {
  transition: all 1s;
  transform: translateY(-100%);
}
.run-enter, .run-leave-to /* .fade-leave-active below version 2.1.8 */ {
  transition: all 0s;
  transform: translateY(100%);
}
</style>
<style lang="less" scoped>
.container {
  width: 100%;
  height: calc(100vh - 45px);
  .cont-bg {
    position: absolute;
    height: calc(100vh - 45px);
    overflow: hidden;
    top: 45px;
    .bg {
      width: 100%;
    }
  }
  .cont {
    width: 94%;
    position: relative;
    padding-bottom: 50px;
    margin: auto;
    .sign {
      border: 2px solid #ffd737;
      border-radius: 5px;
      box-sizing: border-box;
      color: #fff;
      font-size: 16px;
      background: #d72936;
      width: 100%;
      margin: 0 auto;
      text-align: center;
      padding: 10px 0;
      margin-top: 5%;
    }
    .middle {
      position: relative;
      img {
        width: 100%;
      }
      .top-img {
        position: absolute;
        top: 0;
        left: 0;
      }
      .start {
        position: absolute;
        top: 81vw;
        width: 220px;
        left: 70px;
        height: 37px;
      }
      .prise {
        width: 300px;
        height: 100px;
        position: absolute;
        text-align: center;
        left: 50%;
        top: 37%;
        transform: translate(-50%, -0);
        overflow: hidden;
        .leve {
          font-size: 16px;
          color: #d72936;
        }
        .name {
          color: #000;
          font-size: 18px;
          padding-top: 10px;
        }
      }
      .residue {
        text-align: center;
        position: absolute;
        left: 50%;
        top: 83%;
        transform: translate(-50%, -0);
        color: #fff;
        font-size: 14px;
      }
    }
    .rule {
      color: #fff;
      font-size: 14px;
      width: 90%;
      margin: 0 auto;
      .title {
        text-align: center;
        display: flex;
        align-items: center;
        justify-content: center;
        margin-bottom: 5%;
        .border {
          width: 100px;
          height: 1px;
          background: #fff;
        }
        .wz {
          margin: 0 10px;
          font-size: 16px;
        }
      }
      .list {
        padding: 3px 0;
      }
    }
  }
  .fix {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background-color: rgba(0, 0, 0, 0.7);
    z-index: 99;
    .cont-tk {
      position: absolute;
      width: 80%;
      transform: translate(-50%, -50%);
      top: 50%;
      left: 50%;
      background-color: #ffd737;
      img {
        width: 100%;
      }
      .close {
        width: 15%;
        top: 180px;
        left: 43%;
        position: absolute;
      }
      .title {
        padding: 50px 40px;
        font-weight: bold;
        text-align: center;
      }
    }
  }
}
</style>
